{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.1 Hierarchical indexing (MultiIndex)\n",
    "## 13.1.1 Creating a MultiIndex (hierarchical index) object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\Anaconda3\\lib\\importlib\\_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from __future__ import division\n",
    "from numpy.random import randn\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('bar', 'one'),\n",
       " ('bar', 'two'),\n",
       " ('baz', 'one'),\n",
       " ('baz', 'two'),\n",
       " ('foo', 'one'),\n",
       " ('foo', 'two'),\n",
       " ('qux', 'one'),\n",
       " ('qux', 'two')]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],\n",
    "     ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]\n",
    "tuples = list(zip( * arrays))\n",
    "tuples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],\n",
       "           labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],\n",
       "           names=['first', 'second'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "first  second\n",
       "bar    one       1.305335\n",
       "       two       0.967346\n",
       "baz    one       1.496846\n",
       "       two      -0.508762\n",
       "foo    one      -0.515317\n",
       "       two       2.386009\n",
       "qux    one       0.176635\n",
       "       two      -2.290146\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(8), index=index)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],\n",
       "           labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],\n",
       "           names=['first', 'second'])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]\n",
    "pd.MultiIndex.from_product(iterables, names=['first', 'second'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -0.869527\n",
       "     two    3.007375\n",
       "baz  one   -0.463416\n",
       "     two    0.103761\n",
       "foo  one    0.082858\n",
       "     two   -0.164083\n",
       "qux  one   -0.276371\n",
       "     two    0.942808\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),\n",
    "          np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]\n",
    "s = pd.Series(np.random.randn(8), index=arrays)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.528206</td>\n",
       "      <td>0.295614</td>\n",
       "      <td>-1.759369</td>\n",
       "      <td>-0.944767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.314738</td>\n",
       "      <td>-0.306119</td>\n",
       "      <td>1.441279</td>\n",
       "      <td>-0.505352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>1.749754</td>\n",
       "      <td>-0.922271</td>\n",
       "      <td>-0.217271</td>\n",
       "      <td>-0.381560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.851402</td>\n",
       "      <td>0.558540</td>\n",
       "      <td>1.615439</td>\n",
       "      <td>0.116162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.430115</td>\n",
       "      <td>1.298251</td>\n",
       "      <td>0.151380</td>\n",
       "      <td>1.419847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.835240</td>\n",
       "      <td>0.094788</td>\n",
       "      <td>0.158002</td>\n",
       "      <td>-1.018516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.062334</td>\n",
       "      <td>-0.686139</td>\n",
       "      <td>-0.224751</td>\n",
       "      <td>-0.481143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.406869</td>\n",
       "      <td>-0.190647</td>\n",
       "      <td>-0.677544</td>\n",
       "      <td>0.699878</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                0         1         2         3\n",
       "bar one  0.528206  0.295614 -1.759369 -0.944767\n",
       "    two  0.314738 -0.306119  1.441279 -0.505352\n",
       "baz one  1.749754 -0.922271 -0.217271 -0.381560\n",
       "    two  1.851402  0.558540  1.615439  0.116162\n",
       "foo one -0.430115  1.298251  0.151380  1.419847\n",
       "    two  0.835240  0.094788  0.158002 -1.018516\n",
       "qux one  0.062334 -0.686139 -0.224751 -0.481143\n",
       "    two  0.406869 -0.190647 -0.677544  0.699878"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(8, 4), index=arrays)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FrozenList([None, None])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index.names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th colspan=\"2\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"2\" halign=\"left\">baz</th>\n",
       "      <th colspan=\"2\" halign=\"left\">foo</th>\n",
       "      <th colspan=\"2\" halign=\"left\">qux</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.507944</td>\n",
       "      <td>0.850142</td>\n",
       "      <td>-0.127936</td>\n",
       "      <td>-0.305036</td>\n",
       "      <td>-0.322068</td>\n",
       "      <td>2.707971</td>\n",
       "      <td>0.759938</td>\n",
       "      <td>-0.210640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.120355</td>\n",
       "      <td>0.486763</td>\n",
       "      <td>-2.703125</td>\n",
       "      <td>0.573470</td>\n",
       "      <td>0.256857</td>\n",
       "      <td>-0.490830</td>\n",
       "      <td>-1.329649</td>\n",
       "      <td>0.123302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.072873</td>\n",
       "      <td>-1.253435</td>\n",
       "      <td>-0.369442</td>\n",
       "      <td>-0.430883</td>\n",
       "      <td>-1.006108</td>\n",
       "      <td>-0.319729</td>\n",
       "      <td>1.148986</td>\n",
       "      <td>-1.334736</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first        bar                 baz                 foo                 qux  \\\n",
       "second       one       two       one       two       one       two       one   \n",
       "A       0.507944  0.850142 -0.127936 -0.305036 -0.322068  2.707971  0.759938   \n",
       "B      -0.120355  0.486763 -2.703125  0.573470  0.256857 -0.490830 -1.329649   \n",
       "C      -1.072873 -1.253435 -0.369442 -0.430883 -1.006108 -0.319729  1.148986   \n",
       "\n",
       "first             \n",
       "second       two  \n",
       "A      -0.210640  \n",
       "B       0.123302  \n",
       "C      -1.334736  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th colspan=\"2\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"2\" halign=\"left\">baz</th>\n",
       "      <th colspan=\"2\" halign=\"left\">foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.924148</td>\n",
       "      <td>0.452057</td>\n",
       "      <td>0.136893</td>\n",
       "      <td>1.499453</td>\n",
       "      <td>0.973696</td>\n",
       "      <td>-1.334441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.322189</td>\n",
       "      <td>-1.079403</td>\n",
       "      <td>-0.462324</td>\n",
       "      <td>0.624916</td>\n",
       "      <td>-1.010262</td>\n",
       "      <td>0.954554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-1.100605</td>\n",
       "      <td>0.650638</td>\n",
       "      <td>-2.069005</td>\n",
       "      <td>0.200214</td>\n",
       "      <td>-1.614843</td>\n",
       "      <td>-0.046778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.370561</td>\n",
       "      <td>0.642219</td>\n",
       "      <td>0.275243</td>\n",
       "      <td>-0.424944</td>\n",
       "      <td>-1.317498</td>\n",
       "      <td>1.412902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>0.578078</td>\n",
       "      <td>-0.805909</td>\n",
       "      <td>-0.024849</td>\n",
       "      <td>1.145216</td>\n",
       "      <td>-1.416339</td>\n",
       "      <td>-0.145339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.475316</td>\n",
       "      <td>0.003745</td>\n",
       "      <td>-0.058118</td>\n",
       "      <td>0.104301</td>\n",
       "      <td>0.021604</td>\n",
       "      <td>-0.439249</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first              bar                 baz                 foo          \n",
       "second             one       two       one       two       one       two\n",
       "first second                                                            \n",
       "bar   one     0.924148  0.452057  0.136893  1.499453  0.973696 -1.334441\n",
       "      two    -0.322189 -1.079403 -0.462324  0.624916 -1.010262  0.954554\n",
       "baz   one    -1.100605  0.650638 -2.069005  0.200214 -1.614843 -0.046778\n",
       "      two    -0.370561  0.642219  0.275243 -0.424944 -1.317498  1.412902\n",
       "foo   one     0.578078 -0.805909 -0.024849  1.145216 -1.416339 -0.145339\n",
       "      two     0.475316  0.003745 -0.058118  0.104301  0.021604 -0.439249"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " pd.DataFrame(np.random.randn(6, 6), index=index[:6], columns=index[:6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(bar, one)   -1.899847\n",
       "(bar, two)   -0.987172\n",
       "(baz, one)    1.020139\n",
       "(baz, two)   -1.485367\n",
       "(foo, one)    1.908490\n",
       "(foo, two)   -0.259809\n",
       "(qux, one)   -1.079136\n",
       "(qux, two)    1.043130\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(np.random.randn(8), index=tuples)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.1.2 Reconstructing the level labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], dtype='object', name='first')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index.get_level_values(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype='object', name='second')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index.get_level_values('second')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.1.3 Basic indexing on axis with MultiIndex\n",
    ">这段话的意思是沿着MultiIndex所在的轴机械索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.507944</td>\n",
       "      <td>0.850142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.120355</td>\n",
       "      <td>0.486763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.072873</td>\n",
       "      <td>-1.253435</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "second       one       two\n",
       "A       0.507944  0.850142\n",
       "B      -0.120355  0.486763\n",
       "C      -1.072873 -1.253435"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['bar']#输出结构是一个dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.507944\n",
       "B   -0.120355\n",
       "C   -1.072873\n",
       "Name: (bar, one), dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df['bar', 'one']#输出结果是一个serial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.507944\n",
       "B   -0.120355\n",
       "C   -1.072873\n",
       "Name: one, dtype: float64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df['bar']['one']#和上述写法的输出相同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用`df[]`如果要选中多列的话，则写法如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th colspan=\"2\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"2\" halign=\"left\">baz</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.507944</td>\n",
       "      <td>0.850142</td>\n",
       "      <td>-0.127936</td>\n",
       "      <td>-0.305036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.120355</td>\n",
       "      <td>0.486763</td>\n",
       "      <td>-2.703125</td>\n",
       "      <td>0.573470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.072873</td>\n",
       "      <td>-1.253435</td>\n",
       "      <td>-0.369442</td>\n",
       "      <td>-0.430883</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first        bar                 baz          \n",
       "second       one       two       one       two\n",
       "A       0.507944  0.850142 -0.127936 -0.305036\n",
       "B      -0.120355  0.486763 -2.703125  0.573470\n",
       "C      -1.072873 -1.253435 -0.369442 -0.430883"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['bar', 'baz']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.1.4 Defined Levels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],\n",
       "           labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],\n",
       "           names=['first', 'second'])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],\n",
       "           labels=[[2, 2, 3, 3], [0, 1, 0, 1]],\n",
       "           names=['first', 'second'])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['foo','qux']].columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['foo','qux']].columns.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['foo', 'foo', 'qux', 'qux'], dtype='object', name='first')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['foo','qux']].columns.get_level_values(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['foo', 'qux'], ['one', 'two']],\n",
       "           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],\n",
       "           names=['first', 'second'])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['foo','qux']].columns.remove_unused_levels()#低版本可能不支持此方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.1.5 Data alignment and using reindex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -0.869527\n",
       "     two    3.007375\n",
       "baz  one   -0.463416\n",
       "     two    0.103761\n",
       "foo  one    0.082858\n",
       "     two   -0.164083\n",
       "dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[:-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -1.739054\n",
       "     two    6.014750\n",
       "baz  one   -0.926832\n",
       "     two    0.207522\n",
       "foo  one    0.165717\n",
       "     two   -0.328166\n",
       "qux  one         NaN\n",
       "     two         NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s + s[:-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -1.739054\n",
       "     two         NaN\n",
       "baz  one   -0.926832\n",
       "     two         NaN\n",
       "foo  one    0.165717\n",
       "     two         NaN\n",
       "qux  one   -0.552742\n",
       "     two         NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s + s[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "first  second\n",
       "bar    one      -0.869527\n",
       "       two       3.007375\n",
       "baz    one      -0.463416\n",
       "dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " s.reindex(index[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "foo  two   -0.164083\n",
       "bar  one   -0.869527\n",
       "qux  one   -0.276371\n",
       "baz  one   -0.463416\n",
       "dtype: float64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.reindex([('foo', 'two'), ('bar', 'one'), ('qux', 'one'), ('baz', 'one')])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.2 Advanced indexing with hierarchical index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th colspan=\"2\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"2\" halign=\"left\">baz</th>\n",
       "      <th colspan=\"2\" halign=\"left\">foo</th>\n",
       "      <th colspan=\"2\" halign=\"left\">qux</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.507944</td>\n",
       "      <td>0.850142</td>\n",
       "      <td>-0.127936</td>\n",
       "      <td>-0.305036</td>\n",
       "      <td>-0.322068</td>\n",
       "      <td>2.707971</td>\n",
       "      <td>0.759938</td>\n",
       "      <td>-0.210640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.120355</td>\n",
       "      <td>0.486763</td>\n",
       "      <td>-2.703125</td>\n",
       "      <td>0.573470</td>\n",
       "      <td>0.256857</td>\n",
       "      <td>-0.490830</td>\n",
       "      <td>-1.329649</td>\n",
       "      <td>0.123302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.072873</td>\n",
       "      <td>-1.253435</td>\n",
       "      <td>-0.369442</td>\n",
       "      <td>-0.430883</td>\n",
       "      <td>-1.006108</td>\n",
       "      <td>-0.319729</td>\n",
       "      <td>1.148986</td>\n",
       "      <td>-1.334736</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first        bar                 baz                 foo                 qux  \\\n",
       "second       one       two       one       two       one       two       one   \n",
       "A       0.507944  0.850142 -0.127936 -0.305036 -0.322068  2.707971  0.759938   \n",
       "B      -0.120355  0.486763 -2.703125  0.573470  0.256857 -0.490830 -1.329649   \n",
       "C      -1.072873 -1.253435 -0.369442 -0.430883 -1.006108 -0.319729  1.148986   \n",
       "\n",
       "first             \n",
       "second       two  \n",
       "A      -0.210640  \n",
       "B       0.123302  \n",
       "C      -1.334736  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.507944</td>\n",
       "      <td>-0.120355</td>\n",
       "      <td>-1.072873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.850142</td>\n",
       "      <td>0.486763</td>\n",
       "      <td>-1.253435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.127936</td>\n",
       "      <td>-2.703125</td>\n",
       "      <td>-0.369442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.305036</td>\n",
       "      <td>0.573470</td>\n",
       "      <td>-0.430883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.322068</td>\n",
       "      <td>0.256857</td>\n",
       "      <td>-1.006108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2.707971</td>\n",
       "      <td>-0.490830</td>\n",
       "      <td>-0.319729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.759938</td>\n",
       "      <td>-1.329649</td>\n",
       "      <td>1.148986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.210640</td>\n",
       "      <td>0.123302</td>\n",
       "      <td>-1.334736</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "bar   one     0.507944 -0.120355 -1.072873\n",
       "      two     0.850142  0.486763 -1.253435\n",
       "baz   one    -0.127936 -2.703125 -0.369442\n",
       "      two    -0.305036  0.573470 -0.430883\n",
       "foo   one    -0.322068  0.256857 -1.006108\n",
       "      two     2.707971 -0.490830 -0.319729\n",
       "qux   one     0.759938 -1.329649  1.148986\n",
       "      two    -0.210640  0.123302 -1.334736"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.T\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>0.507944</td>\n",
       "      <td>-0.120355</td>\n",
       "      <td>-1.072873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.850142</td>\n",
       "      <td>0.486763</td>\n",
       "      <td>-1.253435</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               A         B         C\n",
       "second                              \n",
       "one     0.507944 -0.120355 -1.072873\n",
       "two     0.850142  0.486763 -1.253435"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['bar']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.850142\n",
       "B    0.486763\n",
       "C   -1.253435\n",
       "Name: (bar, two), dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df.loc['bar', 'two']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.127936</td>\n",
       "      <td>-2.703125</td>\n",
       "      <td>-0.369442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.305036</td>\n",
       "      <td>0.573470</td>\n",
       "      <td>-0.430883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.322068</td>\n",
       "      <td>0.256857</td>\n",
       "      <td>-1.006108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2.707971</td>\n",
       "      <td>-0.490830</td>\n",
       "      <td>-0.319729</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "baz   one    -0.127936 -2.703125 -0.369442\n",
       "      two    -0.305036  0.573470 -0.430883\n",
       "foo   one    -0.322068  0.256857 -1.006108\n",
       "      two     2.707971 -0.490830 -0.319729"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['baz':'foo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>baz</th>\n",
       "      <th>two</th>\n",
       "      <td>-0.305036</td>\n",
       "      <td>0.573470</td>\n",
       "      <td>-0.430883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.322068</td>\n",
       "      <td>0.256857</td>\n",
       "      <td>-1.006108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2.707971</td>\n",
       "      <td>-0.490830</td>\n",
       "      <td>-0.319729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.759938</td>\n",
       "      <td>-1.329649</td>\n",
       "      <td>1.148986</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "baz   two    -0.305036  0.573470 -0.430883\n",
       "foo   one    -0.322068  0.256857 -1.006108\n",
       "      two     2.707971 -0.490830 -0.319729\n",
       "qux   one     0.759938 -1.329649  1.148986"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[('baz', 'two'):('qux', 'one')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>baz</th>\n",
       "      <th>two</th>\n",
       "      <td>-0.305036</td>\n",
       "      <td>0.573470</td>\n",
       "      <td>-0.430883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.322068</td>\n",
       "      <td>0.256857</td>\n",
       "      <td>-1.006108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2.707971</td>\n",
       "      <td>-0.490830</td>\n",
       "      <td>-0.319729</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "baz   two    -0.305036  0.573470 -0.430883\n",
       "foo   one    -0.322068  0.256857 -1.006108\n",
       "      two     2.707971 -0.490830 -0.319729"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[('baz', 'two'):'foo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <th>two</th>\n",
       "      <td>0.850142</td>\n",
       "      <td>0.486763</td>\n",
       "      <td>-1.253435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.759938</td>\n",
       "      <td>-1.329649</td>\n",
       "      <td>1.148986</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "bar   two     0.850142  0.486763 -1.253435\n",
       "qux   one     0.759938 -1.329649  1.148986"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[[('bar', 'two'), ('qux', 'one')]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.2.1 Using slicers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def mklbl(prefix,n):\n",
    "    return [\"%s%s\" % (prefix,i) for i in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "miindex = pd.MultiIndex.from_product([mklbl('A',4),mklbl('B',2),mklbl('C',4),mklbl('D',2)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "micolumns = pd.MultiIndex.from_tuples([('a','foo'),('a','bar'),('b','foo'),('b','bah')],names=['lvl0', 'lvl1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th colspan=\"2\" halign=\"left\">a</th>\n",
       "      <th colspan=\"2\" halign=\"left\">b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "      <th>bah</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">A0</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>9</td>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>13</td>\n",
       "      <td>12</td>\n",
       "      <td>15</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>17</td>\n",
       "      <td>16</td>\n",
       "      <td>19</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>21</td>\n",
       "      <td>20</td>\n",
       "      <td>23</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>25</td>\n",
       "      <td>24</td>\n",
       "      <td>27</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>29</td>\n",
       "      <td>28</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>33</td>\n",
       "      <td>32</td>\n",
       "      <td>35</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>37</td>\n",
       "      <td>36</td>\n",
       "      <td>39</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>41</td>\n",
       "      <td>40</td>\n",
       "      <td>43</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>45</td>\n",
       "      <td>44</td>\n",
       "      <td>47</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>49</td>\n",
       "      <td>48</td>\n",
       "      <td>51</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>53</td>\n",
       "      <td>52</td>\n",
       "      <td>55</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>57</td>\n",
       "      <td>56</td>\n",
       "      <td>59</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>61</td>\n",
       "      <td>60</td>\n",
       "      <td>63</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"14\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>65</td>\n",
       "      <td>64</td>\n",
       "      <td>67</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>69</td>\n",
       "      <td>68</td>\n",
       "      <td>71</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>73</td>\n",
       "      <td>72</td>\n",
       "      <td>75</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>77</td>\n",
       "      <td>76</td>\n",
       "      <td>79</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>81</td>\n",
       "      <td>80</td>\n",
       "      <td>83</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>85</td>\n",
       "      <td>84</td>\n",
       "      <td>87</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>89</td>\n",
       "      <td>88</td>\n",
       "      <td>91</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>93</td>\n",
       "      <td>92</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>97</td>\n",
       "      <td>96</td>\n",
       "      <td>99</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>103</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>105</td>\n",
       "      <td>104</td>\n",
       "      <td>107</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>111</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>113</td>\n",
       "      <td>112</td>\n",
       "      <td>115</td>\n",
       "      <td>114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>117</td>\n",
       "      <td>116</td>\n",
       "      <td>119</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"14\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"6\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>137</td>\n",
       "      <td>136</td>\n",
       "      <td>139</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>141</td>\n",
       "      <td>140</td>\n",
       "      <td>143</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>145</td>\n",
       "      <td>144</td>\n",
       "      <td>147</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>149</td>\n",
       "      <td>148</td>\n",
       "      <td>151</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>153</td>\n",
       "      <td>152</td>\n",
       "      <td>155</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>157</td>\n",
       "      <td>156</td>\n",
       "      <td>159</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>161</td>\n",
       "      <td>160</td>\n",
       "      <td>163</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>165</td>\n",
       "      <td>164</td>\n",
       "      <td>167</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>169</td>\n",
       "      <td>168</td>\n",
       "      <td>171</td>\n",
       "      <td>170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>173</td>\n",
       "      <td>172</td>\n",
       "      <td>175</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>177</td>\n",
       "      <td>176</td>\n",
       "      <td>179</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>181</td>\n",
       "      <td>180</td>\n",
       "      <td>183</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>185</td>\n",
       "      <td>184</td>\n",
       "      <td>187</td>\n",
       "      <td>186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>189</td>\n",
       "      <td>188</td>\n",
       "      <td>191</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>193</td>\n",
       "      <td>192</td>\n",
       "      <td>195</td>\n",
       "      <td>194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>197</td>\n",
       "      <td>196</td>\n",
       "      <td>199</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>201</td>\n",
       "      <td>200</td>\n",
       "      <td>203</td>\n",
       "      <td>202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>205</td>\n",
       "      <td>204</td>\n",
       "      <td>207</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>209</td>\n",
       "      <td>208</td>\n",
       "      <td>211</td>\n",
       "      <td>210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>213</td>\n",
       "      <td>212</td>\n",
       "      <td>215</td>\n",
       "      <td>214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>217</td>\n",
       "      <td>216</td>\n",
       "      <td>219</td>\n",
       "      <td>218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>221</td>\n",
       "      <td>220</td>\n",
       "      <td>223</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>225</td>\n",
       "      <td>224</td>\n",
       "      <td>227</td>\n",
       "      <td>226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>229</td>\n",
       "      <td>228</td>\n",
       "      <td>231</td>\n",
       "      <td>230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>233</td>\n",
       "      <td>232</td>\n",
       "      <td>235</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>237</td>\n",
       "      <td>236</td>\n",
       "      <td>239</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>241</td>\n",
       "      <td>240</td>\n",
       "      <td>243</td>\n",
       "      <td>242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>245</td>\n",
       "      <td>244</td>\n",
       "      <td>247</td>\n",
       "      <td>246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>249</td>\n",
       "      <td>248</td>\n",
       "      <td>251</td>\n",
       "      <td>250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>253</td>\n",
       "      <td>252</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>64 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a         b     \n",
       "lvl1         bar  foo  bah  foo\n",
       "A0 B0 C0 D0    1    0    3    2\n",
       "         D1    5    4    7    6\n",
       "      C1 D0    9    8   11   10\n",
       "         D1   13   12   15   14\n",
       "      C2 D0   17   16   19   18\n",
       "         D1   21   20   23   22\n",
       "      C3 D0   25   24   27   26\n",
       "         D1   29   28   31   30\n",
       "   B1 C0 D0   33   32   35   34\n",
       "         D1   37   36   39   38\n",
       "      C1 D0   41   40   43   42\n",
       "         D1   45   44   47   46\n",
       "      C2 D0   49   48   51   50\n",
       "         D1   53   52   55   54\n",
       "      C3 D0   57   56   59   58\n",
       "         D1   61   60   63   62\n",
       "A1 B0 C0 D0   65   64   67   66\n",
       "         D1   69   68   71   70\n",
       "      C1 D0   73   72   75   74\n",
       "         D1   77   76   79   78\n",
       "      C2 D0   81   80   83   82\n",
       "         D1   85   84   87   86\n",
       "      C3 D0   89   88   91   90\n",
       "         D1   93   92   95   94\n",
       "   B1 C0 D0   97   96   99   98\n",
       "         D1  101  100  103  102\n",
       "      C1 D0  105  104  107  106\n",
       "         D1  109  108  111  110\n",
       "      C2 D0  113  112  115  114\n",
       "         D1  117  116  119  118\n",
       "...          ...  ...  ...  ...\n",
       "A2 B0 C1 D0  137  136  139  138\n",
       "         D1  141  140  143  142\n",
       "      C2 D0  145  144  147  146\n",
       "         D1  149  148  151  150\n",
       "      C3 D0  153  152  155  154\n",
       "         D1  157  156  159  158\n",
       "   B1 C0 D0  161  160  163  162\n",
       "         D1  165  164  167  166\n",
       "      C1 D0  169  168  171  170\n",
       "         D1  173  172  175  174\n",
       "      C2 D0  177  176  179  178\n",
       "         D1  181  180  183  182\n",
       "      C3 D0  185  184  187  186\n",
       "         D1  189  188  191  190\n",
       "A3 B0 C0 D0  193  192  195  194\n",
       "         D1  197  196  199  198\n",
       "      C1 D0  201  200  203  202\n",
       "         D1  205  204  207  206\n",
       "      C2 D0  209  208  211  210\n",
       "         D1  213  212  215  214\n",
       "      C3 D0  217  216  219  218\n",
       "         D1  221  220  223  222\n",
       "   B1 C0 D0  225  224  227  226\n",
       "         D1  229  228  231  230\n",
       "      C1 D0  233  232  235  234\n",
       "         D1  237  236  239  238\n",
       "      C2 D0  241  240  243  242\n",
       "         D1  245  244  247  246\n",
       "      C3 D0  249  248  251  250\n",
       "         D1  253  252  255  254\n",
       "\n",
       "[64 rows x 4 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfmi = pd.DataFrame(np.arange(len(miindex) * len(micolumns)).reshape((len(miindex),len(micolumns))),index=miindex,columns=micolumns).sort_index().sort_index(axis=1)\n",
    "dfmi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th colspan=\"2\" halign=\"left\">a</th>\n",
       "      <th colspan=\"2\" halign=\"left\">b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "      <th>bah</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>73</td>\n",
       "      <td>72</td>\n",
       "      <td>75</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>77</td>\n",
       "      <td>76</td>\n",
       "      <td>79</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>89</td>\n",
       "      <td>88</td>\n",
       "      <td>91</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>93</td>\n",
       "      <td>92</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>105</td>\n",
       "      <td>104</td>\n",
       "      <td>107</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>111</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>121</td>\n",
       "      <td>120</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>125</td>\n",
       "      <td>124</td>\n",
       "      <td>127</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>137</td>\n",
       "      <td>136</td>\n",
       "      <td>139</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>141</td>\n",
       "      <td>140</td>\n",
       "      <td>143</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>153</td>\n",
       "      <td>152</td>\n",
       "      <td>155</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>157</td>\n",
       "      <td>156</td>\n",
       "      <td>159</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>169</td>\n",
       "      <td>168</td>\n",
       "      <td>171</td>\n",
       "      <td>170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>173</td>\n",
       "      <td>172</td>\n",
       "      <td>175</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>185</td>\n",
       "      <td>184</td>\n",
       "      <td>187</td>\n",
       "      <td>186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>189</td>\n",
       "      <td>188</td>\n",
       "      <td>191</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>201</td>\n",
       "      <td>200</td>\n",
       "      <td>203</td>\n",
       "      <td>202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>205</td>\n",
       "      <td>204</td>\n",
       "      <td>207</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>217</td>\n",
       "      <td>216</td>\n",
       "      <td>219</td>\n",
       "      <td>218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>221</td>\n",
       "      <td>220</td>\n",
       "      <td>223</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>233</td>\n",
       "      <td>232</td>\n",
       "      <td>235</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>237</td>\n",
       "      <td>236</td>\n",
       "      <td>239</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>249</td>\n",
       "      <td>248</td>\n",
       "      <td>251</td>\n",
       "      <td>250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>253</td>\n",
       "      <td>252</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a         b     \n",
       "lvl1         bar  foo  bah  foo\n",
       "A1 B0 C1 D0   73   72   75   74\n",
       "         D1   77   76   79   78\n",
       "      C3 D0   89   88   91   90\n",
       "         D1   93   92   95   94\n",
       "   B1 C1 D0  105  104  107  106\n",
       "         D1  109  108  111  110\n",
       "      C3 D0  121  120  123  122\n",
       "         D1  125  124  127  126\n",
       "A2 B0 C1 D0  137  136  139  138\n",
       "         D1  141  140  143  142\n",
       "      C3 D0  153  152  155  154\n",
       "         D1  157  156  159  158\n",
       "   B1 C1 D0  169  168  171  170\n",
       "         D1  173  172  175  174\n",
       "      C3 D0  185  184  187  186\n",
       "         D1  189  188  191  190\n",
       "A3 B0 C1 D0  201  200  203  202\n",
       "         D1  205  204  207  206\n",
       "      C3 D0  217  216  219  218\n",
       "         D1  221  220  223  222\n",
       "   B1 C1 D0  233  232  235  234\n",
       "         D1  237  236  239  238\n",
       "      C3 D0  249  248  251  250\n",
       "         D1  253  252  255  254"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfmi.loc[(slice('A1','A3'), slice(None), ['C1', 'C3']), :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>foo</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A0</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>12</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>24</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>28</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>40</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>44</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>56</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>72</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>76</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>88</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>92</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>104</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>108</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>120</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>124</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>136</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>140</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>152</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>156</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>168</td>\n",
       "      <td>170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>172</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>184</td>\n",
       "      <td>186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>188</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>200</td>\n",
       "      <td>202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>204</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>216</td>\n",
       "      <td>218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>220</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>232</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>236</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>248</td>\n",
       "      <td>250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>252</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a    b\n",
       "lvl1         foo  foo\n",
       "A0 B0 C1 D0    8   10\n",
       "         D1   12   14\n",
       "      C3 D0   24   26\n",
       "         D1   28   30\n",
       "   B1 C1 D0   40   42\n",
       "         D1   44   46\n",
       "      C3 D0   56   58\n",
       "         D1   60   62\n",
       "A1 B0 C1 D0   72   74\n",
       "         D1   76   78\n",
       "      C3 D0   88   90\n",
       "         D1   92   94\n",
       "   B1 C1 D0  104  106\n",
       "         D1  108  110\n",
       "      C3 D0  120  122\n",
       "         D1  124  126\n",
       "A2 B0 C1 D0  136  138\n",
       "         D1  140  142\n",
       "      C3 D0  152  154\n",
       "         D1  156  158\n",
       "   B1 C1 D0  168  170\n",
       "         D1  172  174\n",
       "      C3 D0  184  186\n",
       "         D1  188  190\n",
       "A3 B0 C1 D0  200  202\n",
       "         D1  204  206\n",
       "      C3 D0  216  218\n",
       "         D1  220  222\n",
       "   B1 C1 D0  232  234\n",
       "         D1  236  238\n",
       "      C3 D0  248  250\n",
       "         D1  252  254"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = pd.IndexSlice\n",
    "dfmi.loc[idx[:, :, ['C1', 'C3']], idx[:, 'foo']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>foo</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>64</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>68</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>72</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>76</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>84</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>88</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>92</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>96</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>100</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>104</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>108</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>112</td>\n",
       "      <td>114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>116</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>120</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>124</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0        a    b\n",
       "lvl1      foo  foo\n",
       "B0 C0 D0   64   66\n",
       "      D1   68   70\n",
       "   C1 D0   72   74\n",
       "      D1   76   78\n",
       "   C2 D0   80   82\n",
       "      D1   84   86\n",
       "   C3 D0   88   90\n",
       "      D1   92   94\n",
       "B1 C0 D0   96   98\n",
       "      D1  100  102\n",
       "   C1 D0  104  106\n",
       "      D1  108  110\n",
       "   C2 D0  112  114\n",
       "      D1  116  118\n",
       "   C3 D0  120  122\n",
       "      D1  124  126"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfmi.loc['A1', (slice(None), 'foo')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>foo</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A0</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>12</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>24</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>28</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>40</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>44</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>56</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>72</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>76</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>88</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>92</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>104</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>108</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>120</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>124</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>136</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>140</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>152</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>156</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>168</td>\n",
       "      <td>170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>172</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>184</td>\n",
       "      <td>186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>188</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>200</td>\n",
       "      <td>202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>204</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>216</td>\n",
       "      <td>218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>220</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>232</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>236</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>248</td>\n",
       "      <td>250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>252</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a    b\n",
       "lvl1         foo  foo\n",
       "A0 B0 C1 D0    8   10\n",
       "         D1   12   14\n",
       "      C3 D0   24   26\n",
       "         D1   28   30\n",
       "   B1 C1 D0   40   42\n",
       "         D1   44   46\n",
       "      C3 D0   56   58\n",
       "         D1   60   62\n",
       "A1 B0 C1 D0   72   74\n",
       "         D1   76   78\n",
       "      C3 D0   88   90\n",
       "         D1   92   94\n",
       "   B1 C1 D0  104  106\n",
       "         D1  108  110\n",
       "      C3 D0  120  122\n",
       "         D1  124  126\n",
       "A2 B0 C1 D0  136  138\n",
       "         D1  140  142\n",
       "      C3 D0  152  154\n",
       "         D1  156  158\n",
       "   B1 C1 D0  168  170\n",
       "         D1  172  174\n",
       "      C3 D0  184  186\n",
       "         D1  188  190\n",
       "A3 B0 C1 D0  200  202\n",
       "         D1  204  206\n",
       "      C3 D0  216  218\n",
       "         D1  220  222\n",
       "   B1 C1 D0  232  234\n",
       "         D1  236  238\n",
       "      C3 D0  248  250\n",
       "         D1  252  254"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfmi.loc[idx[:, :, ['C1', 'C3']], idx[:, 'foo']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>foo</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"7\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">B0</th>\n",
       "      <th>C1</th>\n",
       "      <th>D1</th>\n",
       "      <td>204</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>216</td>\n",
       "      <td>218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>220</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>232</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>236</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>248</td>\n",
       "      <td>250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>252</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a    b\n",
       "lvl1         foo  foo\n",
       "A3 B0 C1 D1  204  206\n",
       "      C3 D0  216  218\n",
       "         D1  220  222\n",
       "   B1 C1 D0  232  234\n",
       "         D1  236  238\n",
       "      C3 D0  248  250\n",
       "         D1  252  254"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = dfmi[('a', 'foo')] > 200\n",
    "dfmi.loc[idx[mask, :, ['C1', 'C3']], idx[:, 'foo']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th colspan=\"2\" halign=\"left\">a</th>\n",
       "      <th colspan=\"2\" halign=\"left\">b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "      <th>bah</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A0</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>9</td>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>13</td>\n",
       "      <td>12</td>\n",
       "      <td>15</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>25</td>\n",
       "      <td>24</td>\n",
       "      <td>27</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>29</td>\n",
       "      <td>28</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>41</td>\n",
       "      <td>40</td>\n",
       "      <td>43</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>45</td>\n",
       "      <td>44</td>\n",
       "      <td>47</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>57</td>\n",
       "      <td>56</td>\n",
       "      <td>59</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>61</td>\n",
       "      <td>60</td>\n",
       "      <td>63</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>73</td>\n",
       "      <td>72</td>\n",
       "      <td>75</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>77</td>\n",
       "      <td>76</td>\n",
       "      <td>79</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>89</td>\n",
       "      <td>88</td>\n",
       "      <td>91</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>93</td>\n",
       "      <td>92</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>105</td>\n",
       "      <td>104</td>\n",
       "      <td>107</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>109</td>\n",
       "      <td>108</td>\n",
       "      <td>111</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>121</td>\n",
       "      <td>120</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>125</td>\n",
       "      <td>124</td>\n",
       "      <td>127</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>137</td>\n",
       "      <td>136</td>\n",
       "      <td>139</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>141</td>\n",
       "      <td>140</td>\n",
       "      <td>143</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>153</td>\n",
       "      <td>152</td>\n",
       "      <td>155</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>157</td>\n",
       "      <td>156</td>\n",
       "      <td>159</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>169</td>\n",
       "      <td>168</td>\n",
       "      <td>171</td>\n",
       "      <td>170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>173</td>\n",
       "      <td>172</td>\n",
       "      <td>175</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>185</td>\n",
       "      <td>184</td>\n",
       "      <td>187</td>\n",
       "      <td>186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>189</td>\n",
       "      <td>188</td>\n",
       "      <td>191</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"4\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>201</td>\n",
       "      <td>200</td>\n",
       "      <td>203</td>\n",
       "      <td>202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>205</td>\n",
       "      <td>204</td>\n",
       "      <td>207</td>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>217</td>\n",
       "      <td>216</td>\n",
       "      <td>219</td>\n",
       "      <td>218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>221</td>\n",
       "      <td>220</td>\n",
       "      <td>223</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>233</td>\n",
       "      <td>232</td>\n",
       "      <td>235</td>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>237</td>\n",
       "      <td>236</td>\n",
       "      <td>239</td>\n",
       "      <td>238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>249</td>\n",
       "      <td>248</td>\n",
       "      <td>251</td>\n",
       "      <td>250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>253</td>\n",
       "      <td>252</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a         b     \n",
       "lvl1         bar  foo  bah  foo\n",
       "A0 B0 C1 D0    9    8   11   10\n",
       "         D1   13   12   15   14\n",
       "      C3 D0   25   24   27   26\n",
       "         D1   29   28   31   30\n",
       "   B1 C1 D0   41   40   43   42\n",
       "         D1   45   44   47   46\n",
       "      C3 D0   57   56   59   58\n",
       "         D1   61   60   63   62\n",
       "A1 B0 C1 D0   73   72   75   74\n",
       "         D1   77   76   79   78\n",
       "      C3 D0   89   88   91   90\n",
       "         D1   93   92   95   94\n",
       "   B1 C1 D0  105  104  107  106\n",
       "         D1  109  108  111  110\n",
       "      C3 D0  121  120  123  122\n",
       "         D1  125  124  127  126\n",
       "A2 B0 C1 D0  137  136  139  138\n",
       "         D1  141  140  143  142\n",
       "      C3 D0  153  152  155  154\n",
       "         D1  157  156  159  158\n",
       "   B1 C1 D0  169  168  171  170\n",
       "         D1  173  172  175  174\n",
       "      C3 D0  185  184  187  186\n",
       "         D1  189  188  191  190\n",
       "A3 B0 C1 D0  201  200  203  202\n",
       "         D1  205  204  207  206\n",
       "      C3 D0  217  216  219  218\n",
       "         D1  221  220  223  222\n",
       "   B1 C1 D0  233  232  235  234\n",
       "         D1  237  236  239  238\n",
       "      C3 D0  249  248  251  250\n",
       "         D1  253  252  255  254"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfmi.loc(axis=0)[:, :, ['C1', 'C3']]df2 = dfmi.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th colspan=\"2\" halign=\"left\">a</th>\n",
       "      <th colspan=\"2\" halign=\"left\">b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "      <th>bah</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">A0</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>17</td>\n",
       "      <td>16</td>\n",
       "      <td>19</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>21</td>\n",
       "      <td>20</td>\n",
       "      <td>23</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>33</td>\n",
       "      <td>32</td>\n",
       "      <td>35</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>37</td>\n",
       "      <td>36</td>\n",
       "      <td>39</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>49</td>\n",
       "      <td>48</td>\n",
       "      <td>51</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>53</td>\n",
       "      <td>52</td>\n",
       "      <td>55</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"14\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>65</td>\n",
       "      <td>64</td>\n",
       "      <td>67</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>69</td>\n",
       "      <td>68</td>\n",
       "      <td>71</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>81</td>\n",
       "      <td>80</td>\n",
       "      <td>83</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>85</td>\n",
       "      <td>84</td>\n",
       "      <td>87</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>97</td>\n",
       "      <td>96</td>\n",
       "      <td>99</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>103</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>113</td>\n",
       "      <td>112</td>\n",
       "      <td>115</td>\n",
       "      <td>114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>117</td>\n",
       "      <td>116</td>\n",
       "      <td>119</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"14\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"6\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>145</td>\n",
       "      <td>144</td>\n",
       "      <td>147</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>149</td>\n",
       "      <td>148</td>\n",
       "      <td>151</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>161</td>\n",
       "      <td>160</td>\n",
       "      <td>163</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>165</td>\n",
       "      <td>164</td>\n",
       "      <td>167</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>177</td>\n",
       "      <td>176</td>\n",
       "      <td>179</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>181</td>\n",
       "      <td>180</td>\n",
       "      <td>183</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>193</td>\n",
       "      <td>192</td>\n",
       "      <td>195</td>\n",
       "      <td>194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>197</td>\n",
       "      <td>196</td>\n",
       "      <td>199</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>209</td>\n",
       "      <td>208</td>\n",
       "      <td>211</td>\n",
       "      <td>210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>213</td>\n",
       "      <td>212</td>\n",
       "      <td>215</td>\n",
       "      <td>214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>225</td>\n",
       "      <td>224</td>\n",
       "      <td>227</td>\n",
       "      <td>226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>229</td>\n",
       "      <td>228</td>\n",
       "      <td>231</td>\n",
       "      <td>230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>241</td>\n",
       "      <td>240</td>\n",
       "      <td>243</td>\n",
       "      <td>242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>245</td>\n",
       "      <td>244</td>\n",
       "      <td>247</td>\n",
       "      <td>246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>64 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0           a         b     \n",
       "lvl1         bar  foo  bah  foo\n",
       "A0 B0 C0 D0    1    0    3    2\n",
       "         D1    5    4    7    6\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0   17   16   19   18\n",
       "         D1   21   20   23   22\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "   B1 C0 D0   33   32   35   34\n",
       "         D1   37   36   39   38\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0   49   48   51   50\n",
       "         D1   53   52   55   54\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "A1 B0 C0 D0   65   64   67   66\n",
       "         D1   69   68   71   70\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0   81   80   83   82\n",
       "         D1   85   84   87   86\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "   B1 C0 D0   97   96   99   98\n",
       "         D1  101  100  103  102\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0  113  112  115  114\n",
       "         D1  117  116  119  118\n",
       "...          ...  ...  ...  ...\n",
       "A2 B0 C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0  145  144  147  146\n",
       "         D1  149  148  151  150\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "   B1 C0 D0  161  160  163  162\n",
       "         D1  165  164  167  166\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0  177  176  179  178\n",
       "         D1  181  180  183  182\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "A3 B0 C0 D0  193  192  195  194\n",
       "         D1  197  196  199  198\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0  209  208  211  210\n",
       "         D1  213  212  215  214\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "   B1 C0 D0  225  224  227  226\n",
       "         D1  229  228  231  230\n",
       "      C1 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "      C2 D0  241  240  243  242\n",
       "         D1  245  244  247  246\n",
       "      C3 D0  -10  -10  -10  -10\n",
       "         D1  -10  -10  -10  -10\n",
       "\n",
       "[64 rows x 4 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = dfmi.copy()\n",
    "df2.loc(axis=0)[:, :, ['C1', 'C3']] = -10\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl0</th>\n",
       "      <th colspan=\"2\" halign=\"left\">a</th>\n",
       "      <th colspan=\"2\" halign=\"left\">b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>lvl1</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "      <th>bah</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">A0</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>9000</td>\n",
       "      <td>8000</td>\n",
       "      <td>11000</td>\n",
       "      <td>10000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>13000</td>\n",
       "      <td>12000</td>\n",
       "      <td>15000</td>\n",
       "      <td>14000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>17</td>\n",
       "      <td>16</td>\n",
       "      <td>19</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>21</td>\n",
       "      <td>20</td>\n",
       "      <td>23</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>25000</td>\n",
       "      <td>24000</td>\n",
       "      <td>27000</td>\n",
       "      <td>26000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>29000</td>\n",
       "      <td>28000</td>\n",
       "      <td>31000</td>\n",
       "      <td>30000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>33</td>\n",
       "      <td>32</td>\n",
       "      <td>35</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>37</td>\n",
       "      <td>36</td>\n",
       "      <td>39</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>41000</td>\n",
       "      <td>40000</td>\n",
       "      <td>43000</td>\n",
       "      <td>42000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>45000</td>\n",
       "      <td>44000</td>\n",
       "      <td>47000</td>\n",
       "      <td>46000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>49</td>\n",
       "      <td>48</td>\n",
       "      <td>51</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>53</td>\n",
       "      <td>52</td>\n",
       "      <td>55</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>57000</td>\n",
       "      <td>56000</td>\n",
       "      <td>59000</td>\n",
       "      <td>58000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>61000</td>\n",
       "      <td>60000</td>\n",
       "      <td>63000</td>\n",
       "      <td>62000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"14\" valign=\"top\">A1</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>65</td>\n",
       "      <td>64</td>\n",
       "      <td>67</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>69</td>\n",
       "      <td>68</td>\n",
       "      <td>71</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>73000</td>\n",
       "      <td>72000</td>\n",
       "      <td>75000</td>\n",
       "      <td>74000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>77000</td>\n",
       "      <td>76000</td>\n",
       "      <td>79000</td>\n",
       "      <td>78000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>81</td>\n",
       "      <td>80</td>\n",
       "      <td>83</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>85</td>\n",
       "      <td>84</td>\n",
       "      <td>87</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>89000</td>\n",
       "      <td>88000</td>\n",
       "      <td>91000</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>93000</td>\n",
       "      <td>92000</td>\n",
       "      <td>95000</td>\n",
       "      <td>94000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>97</td>\n",
       "      <td>96</td>\n",
       "      <td>99</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>103</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>105000</td>\n",
       "      <td>104000</td>\n",
       "      <td>107000</td>\n",
       "      <td>106000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>109000</td>\n",
       "      <td>108000</td>\n",
       "      <td>111000</td>\n",
       "      <td>110000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>113</td>\n",
       "      <td>112</td>\n",
       "      <td>115</td>\n",
       "      <td>114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>117</td>\n",
       "      <td>116</td>\n",
       "      <td>119</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"14\" valign=\"top\">A2</th>\n",
       "      <th rowspan=\"6\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>137000</td>\n",
       "      <td>136000</td>\n",
       "      <td>139000</td>\n",
       "      <td>138000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>141000</td>\n",
       "      <td>140000</td>\n",
       "      <td>143000</td>\n",
       "      <td>142000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>145</td>\n",
       "      <td>144</td>\n",
       "      <td>147</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>149</td>\n",
       "      <td>148</td>\n",
       "      <td>151</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>153000</td>\n",
       "      <td>152000</td>\n",
       "      <td>155000</td>\n",
       "      <td>154000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>157000</td>\n",
       "      <td>156000</td>\n",
       "      <td>159000</td>\n",
       "      <td>158000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>161</td>\n",
       "      <td>160</td>\n",
       "      <td>163</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>165</td>\n",
       "      <td>164</td>\n",
       "      <td>167</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>169000</td>\n",
       "      <td>168000</td>\n",
       "      <td>171000</td>\n",
       "      <td>170000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>173000</td>\n",
       "      <td>172000</td>\n",
       "      <td>175000</td>\n",
       "      <td>174000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>177</td>\n",
       "      <td>176</td>\n",
       "      <td>179</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>181</td>\n",
       "      <td>180</td>\n",
       "      <td>183</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>185000</td>\n",
       "      <td>184000</td>\n",
       "      <td>187000</td>\n",
       "      <td>186000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>189000</td>\n",
       "      <td>188000</td>\n",
       "      <td>191000</td>\n",
       "      <td>190000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">A3</th>\n",
       "      <th rowspan=\"8\" valign=\"top\">B0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>193</td>\n",
       "      <td>192</td>\n",
       "      <td>195</td>\n",
       "      <td>194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>197</td>\n",
       "      <td>196</td>\n",
       "      <td>199</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>201000</td>\n",
       "      <td>200000</td>\n",
       "      <td>203000</td>\n",
       "      <td>202000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>205000</td>\n",
       "      <td>204000</td>\n",
       "      <td>207000</td>\n",
       "      <td>206000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>209</td>\n",
       "      <td>208</td>\n",
       "      <td>211</td>\n",
       "      <td>210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>213</td>\n",
       "      <td>212</td>\n",
       "      <td>215</td>\n",
       "      <td>214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>217000</td>\n",
       "      <td>216000</td>\n",
       "      <td>219000</td>\n",
       "      <td>218000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>221000</td>\n",
       "      <td>220000</td>\n",
       "      <td>223000</td>\n",
       "      <td>222000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"8\" valign=\"top\">B1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">C0</th>\n",
       "      <th>D0</th>\n",
       "      <td>225</td>\n",
       "      <td>224</td>\n",
       "      <td>227</td>\n",
       "      <td>226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>229</td>\n",
       "      <td>228</td>\n",
       "      <td>231</td>\n",
       "      <td>230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C1</th>\n",
       "      <th>D0</th>\n",
       "      <td>233000</td>\n",
       "      <td>232000</td>\n",
       "      <td>235000</td>\n",
       "      <td>234000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>237000</td>\n",
       "      <td>236000</td>\n",
       "      <td>239000</td>\n",
       "      <td>238000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C2</th>\n",
       "      <th>D0</th>\n",
       "      <td>241</td>\n",
       "      <td>240</td>\n",
       "      <td>243</td>\n",
       "      <td>242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>245</td>\n",
       "      <td>244</td>\n",
       "      <td>247</td>\n",
       "      <td>246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C3</th>\n",
       "      <th>D0</th>\n",
       "      <td>249000</td>\n",
       "      <td>248000</td>\n",
       "      <td>251000</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1</th>\n",
       "      <td>253000</td>\n",
       "      <td>252000</td>\n",
       "      <td>255000</td>\n",
       "      <td>254000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>64 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "lvl0              a               b        \n",
       "lvl1            bar     foo     bah     foo\n",
       "A0 B0 C0 D0       1       0       3       2\n",
       "         D1       5       4       7       6\n",
       "      C1 D0    9000    8000   11000   10000\n",
       "         D1   13000   12000   15000   14000\n",
       "      C2 D0      17      16      19      18\n",
       "         D1      21      20      23      22\n",
       "      C3 D0   25000   24000   27000   26000\n",
       "         D1   29000   28000   31000   30000\n",
       "   B1 C0 D0      33      32      35      34\n",
       "         D1      37      36      39      38\n",
       "      C1 D0   41000   40000   43000   42000\n",
       "         D1   45000   44000   47000   46000\n",
       "      C2 D0      49      48      51      50\n",
       "         D1      53      52      55      54\n",
       "      C3 D0   57000   56000   59000   58000\n",
       "         D1   61000   60000   63000   62000\n",
       "A1 B0 C0 D0      65      64      67      66\n",
       "         D1      69      68      71      70\n",
       "      C1 D0   73000   72000   75000   74000\n",
       "         D1   77000   76000   79000   78000\n",
       "      C2 D0      81      80      83      82\n",
       "         D1      85      84      87      86\n",
       "      C3 D0   89000   88000   91000   90000\n",
       "         D1   93000   92000   95000   94000\n",
       "   B1 C0 D0      97      96      99      98\n",
       "         D1     101     100     103     102\n",
       "      C1 D0  105000  104000  107000  106000\n",
       "         D1  109000  108000  111000  110000\n",
       "      C2 D0     113     112     115     114\n",
       "         D1     117     116     119     118\n",
       "...             ...     ...     ...     ...\n",
       "A2 B0 C1 D0  137000  136000  139000  138000\n",
       "         D1  141000  140000  143000  142000\n",
       "      C2 D0     145     144     147     146\n",
       "         D1     149     148     151     150\n",
       "      C3 D0  153000  152000  155000  154000\n",
       "         D1  157000  156000  159000  158000\n",
       "   B1 C0 D0     161     160     163     162\n",
       "         D1     165     164     167     166\n",
       "      C1 D0  169000  168000  171000  170000\n",
       "         D1  173000  172000  175000  174000\n",
       "      C2 D0     177     176     179     178\n",
       "         D1     181     180     183     182\n",
       "      C3 D0  185000  184000  187000  186000\n",
       "         D1  189000  188000  191000  190000\n",
       "A3 B0 C0 D0     193     192     195     194\n",
       "         D1     197     196     199     198\n",
       "      C1 D0  201000  200000  203000  202000\n",
       "         D1  205000  204000  207000  206000\n",
       "      C2 D0     209     208     211     210\n",
       "         D1     213     212     215     214\n",
       "      C3 D0  217000  216000  219000  218000\n",
       "         D1  221000  220000  223000  222000\n",
       "   B1 C0 D0     225     224     227     226\n",
       "         D1     229     228     231     230\n",
       "      C1 D0  233000  232000  235000  234000\n",
       "         D1  237000  236000  239000  238000\n",
       "      C2 D0     241     240     243     242\n",
       "         D1     245     244     247     246\n",
       "      C3 D0  249000  248000  251000  250000\n",
       "         D1  253000  252000  255000  254000\n",
       "\n",
       "[64 rows x 4 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = dfmi.copy()\n",
    "df2.loc[idx[:, :, ['C1', 'C3']], :] = df2*1000\n",
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.2.2 Cross-section"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>1.780504</td>\n",
       "      <td>0.437030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.372976</td>\n",
       "      <td>-0.530403</td>\n",
       "      <td>-0.729604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.370736</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>-0.288674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.909506</td>\n",
       "      <td>2.324615</td>\n",
       "      <td>2.109959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>0.999811</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>0.444223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.173751</td>\n",
       "      <td>2.154924</td>\n",
       "      <td>-0.100172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>-1.680412</td>\n",
       "      <td>-1.455452</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.126603</td>\n",
       "      <td>-0.036794</td>\n",
       "      <td>-0.554355</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "bar   one     0.074006  1.780504  0.437030\n",
       "      two    -0.372976 -0.530403 -0.729604\n",
       "baz   one    -0.370736 -0.384049 -0.288674\n",
       "      two    -0.909506  2.324615  2.109959\n",
       "foo   one     0.999811  0.074127  0.444223\n",
       "      two     1.173751  2.154924 -0.100172\n",
       "qux   one    -1.680412 -1.455452 -1.473432\n",
       "      two    -0.126603 -0.036794 -0.554355"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>1.780504</td>\n",
       "      <td>0.437030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>baz</th>\n",
       "      <td>-0.370736</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>-0.288674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <td>0.999811</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>0.444223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>qux</th>\n",
       "      <td>-1.680412</td>\n",
       "      <td>-1.455452</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C\n",
       "first                              \n",
       "bar    0.074006  1.780504  0.437030\n",
       "baz   -0.370736 -0.384049 -0.288674\n",
       "foo    0.999811  0.074127  0.444223\n",
       "qux   -1.680412 -1.455452 -1.473432"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.xs('one', level='second')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>1.780504</td>\n",
       "      <td>0.437030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.370736</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>-0.288674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <th>one</th>\n",
       "      <td>0.999811</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>0.444223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>qux</th>\n",
       "      <th>one</th>\n",
       "      <td>-1.680412</td>\n",
       "      <td>-1.455452</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B         C\n",
       "first second                              \n",
       "bar   one     0.074006  1.780504  0.437030\n",
       "baz   one    -0.370736 -0.384049 -0.288674\n",
       "foo   one     0.999811  0.074127  0.444223\n",
       "qux   one    -1.680412 -1.455452 -1.473432"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[(slice(None),'one'),:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上面两种写法的输出结果来看，虽然选中的数据相同，但是输出结果的结构是并不相同的，第一种结构是普通的index，而第二种是multiindex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>first</th>\n",
       "      <th>bar</th>\n",
       "      <th>baz</th>\n",
       "      <th>foo</th>\n",
       "      <th>qux</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>-0.370736</td>\n",
       "      <td>0.999811</td>\n",
       "      <td>-1.680412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.780504</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>-1.455452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.437030</td>\n",
       "      <td>-0.288674</td>\n",
       "      <td>0.444223</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first       bar       baz       foo       qux\n",
       "A      0.074006 -0.370736  0.999811 -1.680412\n",
       "B      1.780504 -0.384049  0.074127 -1.455452\n",
       "C      0.437030 -0.288674  0.444223 -1.473432"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.T\n",
    "df.xs('one', level='second', axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>bar</th>\n",
       "      <th>baz</th>\n",
       "      <th>foo</th>\n",
       "      <th>qux</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>one</th>\n",
       "      <th>one</th>\n",
       "      <th>one</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>-0.370736</td>\n",
       "      <td>0.999811</td>\n",
       "      <td>-1.680412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.780504</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>-1.455452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.437030</td>\n",
       "      <td>-0.288674</td>\n",
       "      <td>0.444223</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first        bar       baz       foo       qux\n",
       "second       one       one       one       one\n",
       "A       0.074006 -0.370736  0.999811 -1.680412\n",
       "B       1.780504 -0.384049  0.074127 -1.455452\n",
       "C       0.437030 -0.288674  0.444223 -1.473432"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:,(slice(None),'one')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>bar</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.074006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.780504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.437030</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first        bar\n",
       "second       one\n",
       "A       0.074006\n",
       "B       1.780504\n",
       "C       0.437030"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.xs(('one', 'bar'), level=('second', 'first'), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(df.xs(('one', 'bar'), level=('second', 'first'), axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "需要注意这里函数`xs`的入参，第一个入参`('one', 'bar')`其实是和第二个入参`('second', 'first')`相匹配的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can pass drop_level=False to xs() to retain the level that was selected"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>bar</th>\n",
       "      <th>baz</th>\n",
       "      <th>foo</th>\n",
       "      <th>qux</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>one</th>\n",
       "      <th>one</th>\n",
       "      <th>one</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>-0.370736</td>\n",
       "      <td>0.999811</td>\n",
       "      <td>-1.680412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.780504</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>-1.455452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.437030</td>\n",
       "      <td>-0.288674</td>\n",
       "      <td>0.444223</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first        bar       baz       foo       qux\n",
       "second       one       one       one       one\n",
       "A       0.074006 -0.370736  0.999811 -1.680412\n",
       "B       1.780504 -0.384049  0.074127 -1.455452\n",
       "C       0.437030 -0.288674  0.444223 -1.473432"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.xs('one', level='second', axis=1, drop_level=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>first</th>\n",
       "      <th>bar</th>\n",
       "      <th>baz</th>\n",
       "      <th>foo</th>\n",
       "      <th>qux</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.074006</td>\n",
       "      <td>-0.370736</td>\n",
       "      <td>0.999811</td>\n",
       "      <td>-1.680412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.780504</td>\n",
       "      <td>-0.384049</td>\n",
       "      <td>0.074127</td>\n",
       "      <td>-1.455452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.437030</td>\n",
       "      <td>-0.288674</td>\n",
       "      <td>0.444223</td>\n",
       "      <td>-1.473432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first       bar       baz       foo       qux\n",
       "A      0.074006 -0.370736  0.999811 -1.680412\n",
       "B      1.780504 -0.384049  0.074127 -1.455452\n",
       "C      0.437030 -0.288674  0.444223 -1.473432"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.xs('one', level='second', axis=1, drop_level=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.2.3 Advanced reindexing and alignment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "midx = pd.MultiIndex(levels=[['zero', 'one'], ['x','y']],labels=[[1,1,0,0],[1,0,1,0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>y</th>\n",
       "      <td>1.673751</td>\n",
       "      <td>0.570796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>1.176949</td>\n",
       "      <td>-0.109210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">zero</th>\n",
       "      <th>y</th>\n",
       "      <td>-0.608239</td>\n",
       "      <td>0.349932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.407398</td>\n",
       "      <td>0.108706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "one  y  1.673751  0.570796\n",
       "     x  1.176949 -0.109210\n",
       "zero y -0.608239  0.349932\n",
       "     x -0.407398  0.108706"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(4,2), index=midx)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>zero</th>\n",
       "      <td>-0.507819</td>\n",
       "      <td>0.229319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>1.425350</td>\n",
       "      <td>0.230793</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0         1\n",
       "zero -0.507819  0.229319\n",
       "one   1.425350  0.230793"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.mean(level=0)\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>y</th>\n",
       "      <td>1.425350</td>\n",
       "      <td>0.230793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>1.425350</td>\n",
       "      <td>0.230793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">zero</th>\n",
       "      <th>y</th>\n",
       "      <td>-0.507819</td>\n",
       "      <td>0.229319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.507819</td>\n",
       "      <td>0.229319</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "one  y  1.425350  0.230793\n",
       "     x  1.425350  0.230793\n",
       "zero y -0.507819  0.229319\n",
       "     x -0.507819  0.229319"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.reindex(df.index, level=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>y</th>\n",
       "      <td>1.673751</td>\n",
       "      <td>0.570796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>1.176949</td>\n",
       "      <td>-0.109210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">zero</th>\n",
       "      <th>y</th>\n",
       "      <td>-0.608239</td>\n",
       "      <td>0.349932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.407398</td>\n",
       "      <td>0.108706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "one  y  1.673751  0.570796\n",
       "     x  1.176949 -0.109210\n",
       "zero y -0.608239  0.349932\n",
       "     x -0.407398  0.108706"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_aligned, df2_aligned = df.align(df2, level=0)\n",
    "df_aligned"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>y</th>\n",
       "      <td>1.425350</td>\n",
       "      <td>0.230793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>1.425350</td>\n",
       "      <td>0.230793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">zero</th>\n",
       "      <th>y</th>\n",
       "      <td>-0.507819</td>\n",
       "      <td>0.229319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.507819</td>\n",
       "      <td>0.229319</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "one  y  1.425350  0.230793\n",
       "     x  1.425350  0.230793\n",
       "zero y -0.507819  0.229319\n",
       "     x -0.507819  0.229319"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2_aligned"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.2.4 Swapping levels with swaplevel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>y</th>\n",
       "      <td>1.673751</td>\n",
       "      <td>0.570796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>1.176949</td>\n",
       "      <td>-0.109210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">zero</th>\n",
       "      <th>y</th>\n",
       "      <td>-0.608239</td>\n",
       "      <td>0.349932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.407398</td>\n",
       "      <td>0.108706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "one  y  1.673751  0.570796\n",
       "     x  1.176949 -0.109210\n",
       "zero y -0.608239  0.349932\n",
       "     x -0.407398  0.108706"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <th>one</th>\n",
       "      <td>1.673751</td>\n",
       "      <td>0.570796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <th>one</th>\n",
       "      <td>1.176949</td>\n",
       "      <td>-0.109210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <th>zero</th>\n",
       "      <td>-0.608239</td>\n",
       "      <td>0.349932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <th>zero</th>\n",
       "      <td>-0.407398</td>\n",
       "      <td>0.108706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "y one   1.673751  0.570796\n",
       "x one   1.176949 -0.109210\n",
       "y zero -0.608239  0.349932\n",
       "x zero -0.407398  0.108706"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df[:5].swaplevel(0, 1, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.2.5 Reordering levels with reorder_levels()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <th>one</th>\n",
       "      <td>1.673751</td>\n",
       "      <td>0.570796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <th>one</th>\n",
       "      <td>1.176949</td>\n",
       "      <td>-0.109210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <th>zero</th>\n",
       "      <td>-0.608239</td>\n",
       "      <td>0.349932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <th>zero</th>\n",
       "      <td>-0.407398</td>\n",
       "      <td>0.108706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               0         1\n",
       "y one   1.673751  0.570796\n",
       "x one   1.176949 -0.109210\n",
       "y zero -0.608239  0.349932\n",
       "x zero -0.407398  0.108706"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[:5].reorder_levels([1,0], axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.3 Sorting a MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    " import random; random.shuffle(tuples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "baz  one    0.818091\n",
       "foo  two    0.356114\n",
       "     one    0.497309\n",
       "qux  one    0.661342\n",
       "baz  two   -0.374351\n",
       "bar  two    0.412466\n",
       "     one   -0.300108\n",
       "qux  two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(8), index=pd.MultiIndex.from_tuples(tuples))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -0.300108\n",
       "     two    0.412466\n",
       "baz  one    0.818091\n",
       "     two   -0.374351\n",
       "foo  one    0.497309\n",
       "     two    0.356114\n",
       "qux  one    0.661342\n",
       "     two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " s.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -0.300108\n",
       "     two    0.412466\n",
       "baz  one    0.818091\n",
       "     two   -0.374351\n",
       "foo  one    0.497309\n",
       "     two    0.356114\n",
       "qux  one    0.661342\n",
       "     two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.sort_index(level=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bar  one   -0.300108\n",
       "baz  one    0.818091\n",
       "foo  one    0.497309\n",
       "qux  one    0.661342\n",
       "bar  two    0.412466\n",
       "baz  two   -0.374351\n",
       "foo  two    0.356114\n",
       "qux  two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " s.sort_index(level=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "L1   L2 \n",
       "baz  one    0.818091\n",
       "foo  two    0.356114\n",
       "     one    0.497309\n",
       "qux  one    0.661342\n",
       "baz  two   -0.374351\n",
       "bar  two    0.412466\n",
       "     one   -0.300108\n",
       "qux  two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index.set_names(['L1', 'L2'], inplace=True)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "L1   L2 \n",
       "bar  one   -0.300108\n",
       "     two    0.412466\n",
       "baz  one    0.818091\n",
       "     two   -0.374351\n",
       "foo  one    0.497309\n",
       "     two    0.356114\n",
       "qux  one    0.661342\n",
       "     two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " s.sort_index(level='L1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "L1   L2 \n",
       "bar  one   -0.300108\n",
       "baz  one    0.818091\n",
       "foo  one    0.497309\n",
       "qux  one    0.661342\n",
       "bar  two    0.412466\n",
       "baz  two   -0.374351\n",
       "foo  two    0.356114\n",
       "qux  two   -0.470887\n",
       "dtype: float64"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " s.sort_index(level='L2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>zero</th>\n",
       "      <th>one</th>\n",
       "      <th>zero</th>\n",
       "      <th>one</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.407398</td>\n",
       "      <td>1.176949</td>\n",
       "      <td>-0.608239</td>\n",
       "      <td>1.673751</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.108706</td>\n",
       "      <td>-0.109210</td>\n",
       "      <td>0.349932</td>\n",
       "      <td>0.570796</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       zero       one      zero       one\n",
       "          x         x         y         y\n",
       "0 -0.407398  1.176949 -0.608239  1.673751\n",
       "1  0.108706 -0.109210  0.349932  0.570796"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df.T.sort_index(level=1, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>jolie</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jim</th>\n",
       "      <th>joe</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0</th>\n",
       "      <th>x</th>\n",
       "      <td>0.761517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>0.110394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>z</th>\n",
       "      <td>0.896214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>0.680846</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            jolie\n",
       "jim joe          \n",
       "0   x    0.761517\n",
       "    x    0.110394\n",
       "1   z    0.896214\n",
       "    y    0.680846"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfm = pd.DataFrame({'jim': [0, 0, 1, 1],\n",
    "'joe': ['x', 'x', 'z', 'y'],\n",
    "'jolie': np.random.rand(4)})\n",
    "dfm = dfm.set_index(['jim', 'joe'])\n",
    "dfm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:1: PerformanceWarning: indexing past lexsort depth may impact performance.\n",
      "  if __name__ == '__main__':\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>jolie</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jim</th>\n",
       "      <th>joe</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>z</th>\n",
       "      <td>0.896214</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            jolie\n",
       "jim joe          \n",
       "1   z    0.896214"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfm.loc[(1, 'z')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " dfm.index.is_lexsorted()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " dfm.index.lexsort_depth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>jolie</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jim</th>\n",
       "      <th>joe</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0</th>\n",
       "      <th>x</th>\n",
       "      <td>0.761517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>0.110394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>y</th>\n",
       "      <td>0.680846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>0.896214</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            jolie\n",
       "jim joe          \n",
       "0   x    0.761517\n",
       "    x    0.110394\n",
       "1   y    0.680846\n",
       "    z    0.896214"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfm = dfm.sort_index()\n",
    "dfm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfm.index.is_lexsorted()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfm.index.lexsort_depth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>jolie</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>jim</th>\n",
       "      <th>joe</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>y</th>\n",
       "      <td>0.680846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>0.896214</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            jolie\n",
       "jim joe          \n",
       "1   y    0.680846\n",
       "    z    0.896214"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfm.loc[(0,'y'):(1, 'z')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.4 Take Methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([595, 897, 608, 353, 929, 395, 372, 872, 751, 815], dtype='int64')"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.Index(np.random.randint(0, 1000, 10))\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "positions = [0, 9, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([595, 815, 353], dtype='int64')"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index[positions]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([595, 815, 353], dtype='int64')"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index.take(positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ser = pd.Series(np.random.randn(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.661064\n",
       "9   -0.014501\n",
       "3    1.234987\n",
       "dtype: float64"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.iloc[positions]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.661064\n",
       "9   -0.014501\n",
       "3    1.234987\n",
       "dtype: float64"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.take(positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "frm = pd.DataFrame(np.random.randn(5, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.832012</td>\n",
       "      <td>0.799553</td>\n",
       "      <td>0.848477</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.927942</td>\n",
       "      <td>-0.144121</td>\n",
       "      <td>1.015594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.652196</td>\n",
       "      <td>0.601819</td>\n",
       "      <td>-2.059418</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2\n",
       "1 -0.832012  0.799553  0.848477\n",
       "4  1.927942 -0.144121  1.015594\n",
       "3 -0.652196  0.601819 -2.059418"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frm.take([1, 4, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.464378</td>\n",
       "      <td>0.089220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.832012</td>\n",
       "      <td>0.848477</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.804328</td>\n",
       "      <td>1.267339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.652196</td>\n",
       "      <td>-2.059418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.927942</td>\n",
       "      <td>1.015594</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         2\n",
       "0 -0.464378  0.089220\n",
       "1 -0.832012  0.848477\n",
       "2  0.804328  1.267339\n",
       "3 -0.652196 -2.059418\n",
       "4  1.927942  1.015594"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " frm.take([0, 2], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.7923, -0.3155,  1.157 , -0.7945,  1.8619,  0.9373, -1.4501,\n",
       "        1.8907, -0.5277, -0.5532])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.7923, -0.7923, -0.3155, -0.3155])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.take([False, False, True, True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.7923, -0.3155])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[[0, 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.397248\n",
       "1   -2.082079\n",
       "2   -0.234860\n",
       "3   -1.605896\n",
       "4    0.995013\n",
       "5    0.536290\n",
       "6    0.172954\n",
       "7   -2.712020\n",
       "8    0.274945\n",
       "9    1.258128\n",
       "dtype: float64"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = pd.Series(np.random.randn(10))\n",
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   -0.152240\n",
       "0   -0.152240\n",
       "1   -0.565602\n",
       "1   -0.565602\n",
       "dtype: float64"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.take([False, False, True, True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.397248\n",
       "1   -2.082079\n",
       "dtype: float64"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.iloc[[0, 1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这种情况下，False表示的是0，True表示的是1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.5 Index Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pandas的index类型如下：\n",
    "- Int64Index\n",
    "- Float64Index\n",
    "- RangeIndex\n",
    "- IntervalIndexS\n",
    "- MultiIndex\n",
    "- DatetimeIndex\n",
    "- PeriodIndex\n",
    "- TimedeltaIndex\n",
    "- CategoricalIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.5.1 CategoricalIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named 'pandas.api'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-121-087969896329>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtypes\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mCategoricalDtype\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'B'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'aabbca'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: No module named 'pandas.api'"
     ]
    }
   ],
   "source": [
    "#from pandas.api.types import CategoricalDtype\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "0  0  a\n",
       "1  1  a\n",
       "2  2  b\n",
       "3  3  b\n",
       "4  4  c\n",
       "5  5  a"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A': np.arange(6),'B': list('aabbca')})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "0  0  a\n",
       "1  1  a\n",
       "2  2  b\n",
       "3  3  b\n",
       "4  4  c\n",
       "5  5  a"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['B'] = df['B'].astype('category', categories=list('cab'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A       int32\n",
       "B    category\n",
       "dtype: object"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['c', 'a', 'b'], dtype='object')"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.B.cat.categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A\n",
       "B   \n",
       "a  0\n",
       "a  1\n",
       "b  2\n",
       "b  3\n",
       "c  4\n",
       "a  5"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.set_index('B')\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CategoricalIndex(['a', 'a', 'b', 'b', 'c', 'a'], categories=['c', 'a', 'b'], ordered=False, name='B', dtype='category')"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A\n",
       "B   \n",
       "c  4\n",
       "a  0\n",
       "a  1\n",
       "a  5\n",
       "b  2\n",
       "b  3"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A\n",
       "B   \n",
       "c  4\n",
       "a  6\n",
       "b  5"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.groupby(level=0).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CategoricalIndex(['c', 'a', 'b'], categories=['c', 'a', 'b'], ordered=False, name='B', dtype='category')"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.groupby(level=0).sum().index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reindexing operations, will return a resulting index based on the type of the passed indexer, meaning that passing\n",
    "a list will return a plain-old-Index; indexing with a Categorical will return a CategoricalIndex, indexed\n",
    "according to the categories of the PASSED Categorical dtype. This allows one to arbitrarily index these even\n",
    "with values NOT in the categories, similarly to how you can reindex ANY pandas index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A\n",
       "B     \n",
       "a  0.0\n",
       "a  1.0\n",
       "a  5.0\n",
       "e  NaN"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df2.reindex(['a','e'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'a', 'a', 'e'], dtype='object', name='B')"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df2.reindex(['a','e']).index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A\n",
       "B     \n",
       "a  0.0\n",
       "a  1.0\n",
       "a  5.0\n",
       "e  NaN"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df2.reindex(pd.Categorical(['a','e'],categories=list('abcde')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CategoricalIndex(['a', 'a', 'a', 'e'], categories=['a', 'b', 'c', 'd', 'e'], ordered=False, name='B', dtype='category')"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df2.reindex(pd.Categorical(['a','e'],categories=list('abcde'))).index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.5.2 Int64Index and RangeIndex"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
